The C++ Part of the Test

with 75 points to be counted as 35% of the grade on this test

(As a bonus there will be occasional tips for your personal life )

 

1. Constants, variables, and types: which take up memory? (2)

 

 

 

 

2. What is the output of the following code fragment? (Be Careful and explain your answer!) (2)

 

int num1 = 5;

int num2 = 2;

double result;

result = num1 / num2;

cout << result;

 

 

 

 

3. Review the code below and then draw the pictures of how memory looks at the three places noted in the comments. Be sure to label each picture and include all relevant memory each time. (6)

 

double CalcAvg(double& total, int count);

 

void main()

{ int cnt = 5;

double sum = 20;

double average;

// PICTURE 1

 

average = CalcAvg(total, cnt);

// PICTURE 3

}

 

double CalcAvg(double& total, int count)

{ // PICTURE 2

total = 25;

return total/count;

}

 

4. Write a program (no functions required) that counts the number of times a character is entered by a user before the character ‘x’ is entered. (5)

 

 

 

 

 

 

 

 

You may need a break from if you stare at an orange juice container because it says, "CONCENTRATE."

5. Write the test condition for a while loop that has the loop stop if the value 11 or 12 is entered. (2)

 

 

 

6. What is the difference between a semantic error and a syntactic error? (2)

 

 

 

 

7. Trace the following code. Be sure to include the output in the trace (5)

const int MAX = 4;

for (int i=0; i<MAX; i++)

{ cout << i+1;

for (int j = 0; j < i+1; j++)

{ cout << ‘*’;

}

cout << endl;

}

 

 

 

 

 

 

8. It is TRUE that either NOT all ‘if’ statements can be converted to ‘switch’ statements or NOT all ‘switch’ statements can be converted to ‘if’ statements. Choose which of these is correct and give an example of ‘if’ or ‘switch’ code that cannot be converted. Finish with an explanation of why the conversion cannot take place. (4)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9. If we are describing what the word ‘dog’ means to someone who does not speak English, we are describing:

a) a class

b) an object

c) an instance

d) a decomposition

e) a CRC

[Circle your answer] (2)

 

You may want to examine the time you spend with a computer if you only receive jokes through email

 

9. Write a structure chart that corresponds to the following nonsense code. (4)

char GetA();

char GetB();

void DoIt1();

void DoIt2();

void ReadA();

void ReadB();

 

main()

{ char ch1 = GetA();

char ch2;

for (int j = 0; j < 3; j++)

{ ch1 = GetB();

ch2 = GetA();

}

}

 

char GetA()

{ DoIt1();

ReadA();

}

 

char GetB()

{ int x ;

cout << "Please enter a number";

cin >> x;

if (x != 2)

{ DoIt2();

ReadA();

}

ReadB();

}

 

10. A program needs a class called ‘PC’ with properties for CPU speed, disk space, floppy size (3 ½ or 5 Ό), and type (‘L’ for laptop, ‘D’ for desktop, and ‘T’ for tower). All properties are accessible and changeable.

 

a) Declare this class. Include a constructor with parameters for all properties (3)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b) Define the constructor declared in ‘a’. (2)

 

 

 

 

 

c) Declare an instance of this class. (2)

 

 

d) Declare a type representing an array of 5 instances of this class (2)

 

 

 

e) Use the type declared in ‘d’ to declare an array of instances of this class. (2)

 

 

 

f) If your answer to parts ‘b’, ‘c’. ‘d’. or ‘e’ require a change in the original class declaration in ‘a’, put the change here. (2)

 

 

 

g) Draw pictures of how memory will look after a computer encounters the code you wrote for:

‘c’ (2)

 

 

 

‘d’ (2)

 

 

 

‘e’ (2)

 

 

11. Arrays

a) Declare an array of 10 doubles (2)

 

 

b) Write a function that receives an instance of this array and fills it with values entered by a user. (5)

 

 

 

 

 

 

 

 

 

 

 

 

You may need to check with an auto mechanic if you rotate your screen savers more often than your tires

 

12. Lists

a) Declare the private part of a list of PC’s using the implementation found in your last program (3)

 

 

 

 

 

 

b) Declare a member function that adds an instance of PC to the list declared in ‘a’ (2)

 

 

 

c) Define the constructor this list class. (2)

 

 

 

 

13. Draw pictures representing memory when requested in the instructions below. Note: a few of these instructions may be illegal. If you find an illegal instruction, simply write "ILLEGAL" next to it and skip that instruction when drawing your pictures. (8)

 

double* p1;

double* p2;

double* p3;

char* p4;

// Picture 1

 

p1 = new double;

p3 = new double;

p4 = new double;

// Picture 2

 

*p1 = 10;

p2 = 10;

p3 = 0;

char* p5 = new char;

p4 = p5;

// Picture 3

 

*p3 = *p1 + 5;

*p4 = *p1;

// Picture 4